using System;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ResumeSearchResult : System.Web.UI.Page
{
    int pageSize = 4;
    ControlHelper controlHelper;
    string connectionString =
            WebConfigurationManager.ConnectionStrings[
            "RCVDB2000"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal1.Text="";        
        controlHelper = new
        ControlHelper(connectionString);
        controlHelper.CommandText =
        "select JobPosition, "+ 
        " JobCategoryName, City, "+ 
        " ExperienceYear, SalaryFrom,"+ 
        " SalaryTo, Currency "+ 
        " from tblJSResumes R, "+ 
        " tblJSResumeDetails D, "+ 
        " tblJobCategories C "+ 
        " where R.ResumeID=D.ResumeID "+
        " and D.JobCategoryID = C.JobCategoryID ";
        Page prevousPage= this.PreviousPage;
        string whereClause="";
        
        if (prevousPage  != null)
        {
            TextBox textBox = (TextBox)prevousPage.FindControl("txtPosition");
            if(textBox.Text != "")
            {
                txtPosition.Value = textBox.Text;
                whereClause += " and JobPosition like '%" + textBox.Text + "%'";
            }
            textBox = (TextBox)prevousPage.FindControl("txtCity");
            if(textBox.Text != "")
            {
                txtCity.Value = textBox.Text;
                whereClause += " and City like '%" + textBox.Text + "%'";
            }
            textBox = (TextBox)prevousPage.FindControl("txtSalaryFrom");
            if(textBox.Text != "")
            {
                txtSalaryFrom.Value = textBox.Text;
                whereClause += " and SalaryFrom >= " + textBox.Text  ;
            }
            textBox = (TextBox)prevousPage.FindControl("txtSalaryTo");
            if(textBox.Text != "")
            {
                txtSalaryTo.Value = textBox.Text;
                whereClause += " and SalaryTo <= " + textBox.Text;
            }
            DropDownList dropDownList = (DropDownList)prevousPage.FindControl("ddlCurrency");
            if(textBox.Text != "")
            {
                txtCurrency.Value = dropDownList.SelectedValue;
                whereClause += " and Currency = '" + dropDownList.SelectedValue + "'";
            }
            
        }
        else
        {
            if (txtPosition.Value != "")
            {
                whereClause += " and JobPosition like '%" + txtPosition.Value  + "%'";
            }
            if (txtCity.Value != "")
            {
                 
                whereClause += " and City like '%" + txtCity.Value  + "%'";
            }

            if (txtSalaryFrom.Value != "")
            {
                whereClause += " and SalaryFrom >= " + txtSalaryFrom.Value;
            }

            if (txtSalaryTo.Value != "")
            {
                 
                whereClause += " and SalaryTo <= " + txtSalaryTo.Value;
            }

            if (txtCurrency.Value != "")
            {

                whereClause += " and Currency = '" + txtCurrency.Value +"'";
            }
        }
        if(whereClause == "" )
            Response.Redirect("ResumeSearch.aspx");
        controlHelper.CommandText += whereClause;
        controlHelper.CommandType =
        CommandType.Text;
        controlHelper.InitProperties();
        if (prevousPage  != null)
        {
            controlHelper.FillDataTableToDataList(
            this.DataList1, 0, pageSize);
        
            lblPage.Text = "1";
             
            controlHelper.CommandText =
            "select count(*) from tblJSResumes R, "+ 
            " tblJSResumeDetails D, "+ 
            " tblJobCategories C "+ 
            " where R.ResumeID=D.ResumeID "+
            " and D.JobCategoryID = C.JobCategoryID ";
            controlHelper.CommandText += whereClause;
            controlHelper.CommandType = CommandType.Text;
            controlHelper.InitProperties();
            int rows = controlHelper.GetTotalRecords();
            lblPages.Text = (rows / pageSize
            + ((rows % pageSize) > 0 ? 1 : 0)).ToString();
             
            if (rows <= pageSize)
            {
                linkPrevious.Visible = false;
                linkFirst.Visible = false;
                linkLast.Visible = false;
                linkNext.Visible = false;
            }
            else
            {
                linkPrevious.Enabled = false;
                linkFirst.Enabled = false;
            }  
        }      
    }


    protected void linkFirst_Click(object sender, EventArgs e)
    {

        controlHelper.FillDataTableToDataList(this.DataList1, 0, pageSize);
        lblPage.Text = "1";
        
        linkPrevious.Enabled = false;
        linkFirst.Enabled = false;
        linkNext.Enabled = true;
        linkLast.Enabled = true;
    }
    protected void linkPrevious_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = true;
        linkLast.Enabled = true;
        int currentPage =
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 1) currentPage--;

        controlHelper.FillDataTableToDataList(
        this.DataList1, (currentPage - 1) * 
        pageSize, pageSize);
        lblPage.Text = currentPage.ToString();
         
        if (currentPage == 1)
        {
            linkPrevious.Enabled = false;
            linkFirst.Enabled = false;
        }
        else
        {
            linkFirst.Enabled = true;
            linkPrevious.Enabled = true;
        }
    }

    protected void linkNext_Click(object sender, EventArgs e)
    {
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;
        int currentPage =
        Convert.ToInt32(lblPage.Text);
        if (currentPage > 0) currentPage++;

        controlHelper.FillDataTableToDataList(
        this.DataList1, (currentPage - 1) * pageSize, pageSize);
        lblPage.Text = currentPage.ToString();
        
        if (currentPage ==
        Convert.ToInt32(lblPages.Text))
        {
            linkLast.Enabled = false;
            linkNext.Enabled = false;
        }
        else
        {
            linkLast.Enabled = true;
            linkNext.Enabled = true;
        }
    }


    protected void linkLast_Click(object sender, EventArgs e)
    {
        linkNext.Enabled = false;
        linkLast.Enabled = false;
        linkFirst.Enabled = true;
        linkPrevious.Enabled = true;

        controlHelper.FillDataTableToDataList(
        this.DataList1, (Convert.ToInt32(lblPages.Text)
        - 1) * pageSize, pageSize);
        lblPage.Text = lblPages.Text;
         
    }
     
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataListItem dataListItem = e.Item;
        Label label = (Label) dataListItem.FindControl("lblPosition");
        Literal1.Text = "You just added \"" + label.Text +"\" to cart";
        
    }
}
